package Solution.problem053;

/**
 * @program Leetcode
 * @description: 更好的解法
 * @author: lishangsheng
 * @create: 2019/05/28 09:47
 */
public class BetterSolution {

    public int maxSubArray(int[] nums) {
        int size = nums.length;
        int max_so_far = Integer.MIN_VALUE, max_ending_here = 0;

        for (int i = 0; i < size; i++) {
            max_ending_here = max_ending_here + nums[i];
            if (max_so_far < max_ending_here)
                max_so_far = max_ending_here;
            if (max_ending_here < 0)
                max_ending_here = 0;
        }
        return max_so_far;
    }
}
